#!/usr/bin/python
#-*- coding:utf-8 -*-
import sys, webbrowser, os, keyword, wx, classes, lib.functions, functions, lib.classes
from wx import stc

if wx.Platform == "__WXMSW__":
	faces = {'times': 'Times New Roman', 'mono' : 'Courier New', 'helv' : 'Arial', 'other': 'Comic Sans MS', 'size' : 10, 'size2': 8}
else:
	faces = {'times': 'Times', 'mono' : 'Courier', 'helv' : 'Helvetica', 'other': 'new century schoolbook', 'size' : 12, 'size2': 10}
class Frame(wx.Frame):
	if wx.Platform == "__WXMSW__":
		faces = {'times': 'Times New Roman', 'mono' : 'Courier New', 'helv' : 'Arial', 'other': 'Comic Sans MS', 'size' : 10, 'size2': 8}
	else:
		faces = {'times': 'Times', 'mono' : 'Courier', 'helv' : 'Helvetica', 'other': 'new century schoolbook', 'size' : 12, 'size2': 10}

	cpp_key = "and and_eq asm auto bitand bitor bool break case catch char class compl const const_cast continue \
default delete do double dynamic_cast else enum explicit export extern false float for \
friend goto if inline int long mutable namespace new not not_eq \
operator or or_eq private protected public \
register reinterpret_cast return short signed sizeof static static_cast struct switch \
template this throw true try typedef typeid typename union unsigned using \
virtual void volatile wchar_t while xor xor_eq"
	def __init__(self, version, parent=None, id=wx.NewId(), title="wxEdit", pos=(0, 0), size=(600, 500), author="Alessio 'Alexei95' Colucci", email="alessio.colucci@live.it", site="http://code.google.com/p/wxsoft"):
		self.tskic = None
		self.cfg = classes.Config()
		self.prefix1, self.prefix2, self.prefix3 = lib.functions.parse_path(sys.argv)
		id1 = wx.NewId()
		id2 = wx.NewId()
		id3 = wx.NewId()
		id4 = wx.NewId()
		id5 = wx.NewId()
		self.id6 = wx.NewId()
		self.id7 = wx.NewId()
		id8 = wx.NewId()
		id9 = wx.NewId()
		id10 = wx.NewId()
		id11 = wx.NewId()
		id12 = wx.NewId()
		id13 = wx.NewId()
		self.redo_id = wx.NewId()
		self.undo_id = wx.NewId()
		self.cut_id = wx.NewId()
		self.copy_id = wx.NewId()
		self.paste_id = wx.NewId()
		self.select_all_id = wx.NewId()
		self.checked = True
		self.checked2 = True
		self.doc = 1
		self.name = "Untitled"
		self.untitled = 1
		self.home = os.path.join(os.path.expanduser("~"), "")
		self.version = version
		self.author = author
		self.email = email
		self.site = site
		
		wx.Frame.__init__(self, parent, id, title, pos, size)
		self.SetMinSize((520, 165))
		if self.cfg.cfg.getboolean("Window", "Maximized"):
			self.Maximize()
		self.SetIcon(wx.Icon(self.prefix3+'wxedit.png', wx.BITMAP_TYPE_PNG))
		self.statusbar = self.CreateStatusBar(2)
		menubar = wx.MenuBar()
		
		file = wx.Menu()
		new = wx.MenuItem(file, wx.NewId(), "&New\tCtrl+N", "Open a new file")
		new.SetBitmap(wx.Image(self.prefix1+'document-new.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap())
		file.AppendItem(new)
		save = wx.MenuItem(file, wx.NewId(), "&Save\tCtrl+S", "Save the current file")
		save.SetBitmap(wx.Image(self.prefix1+'document-save.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap())
		file.AppendItem(save)
		save_as = wx.MenuItem(file, wx.NewId(), "&Save as...\tCtrl+Shift+S", "Save the current file under a new name")
		save_as.SetBitmap(wx.Image(self.prefix1+'document-save-as.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap())
		file.AppendItem(save_as)
		save_all = wx.MenuItem(file, wx.NewId(), "&Save All", "Save all the files")
		file.AppendItem(save_all)
		open = wx.MenuItem(file, wx.NewId(), "&Open...\tCtrl+O", "Open a file")
		open.SetBitmap(wx.Image(self.prefix1+'document-open.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap())
		file.AppendItem(open)
		close = wx.MenuItem(file, wx.NewId(), "&Close\tCtrl+W", "Close the current tab")
		close.SetBitmap(wx.Image(self.prefix1+'window-close.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap())
		file.AppendItem(close)
		close_all = wx.MenuItem(file, wx.NewId(), "&Close All", "Close all the files")
		file.AppendItem(close_all)
		file.AppendSeparator()
		save_session = wx.MenuItem(file, wx.NewId(), "&Save Session...", "Save the current session")
		file.AppendItem(save_session)
		load_session = wx.MenuItem(file, wx.NewId(), "&Load Session...", "Load a saved session")
		file.AppendItem(load_session)
		file.AppendSeparator()
		save_preferences = wx.MenuItem(file, wx.NewId(), "&Save Preferences...", "Save the preferences in a file")
		file.AppendItem(save_preferences)
		load_preferences = wx.MenuItem(file, wx.NewId(), "&Load Preferences...", "Load the preferences from a file")
		file.AppendItem(load_preferences)
		reload_preferences = wx.MenuItem(file, wx.NewId(), "&Re-load Preferences", "Re-load the default preferences")
		file.AppendItem(reload_preferences)
		oof = wx.MenuItem(file, wx.NewId(), "&Open Option File", "Open the options file in a new tab")
		file.AppendItem(oof)
		file.AppendSeparator()
		quit = wx.MenuItem(file, wx.NewId(), "&Quit\tCtrl+Q", "Close the program")
		quit.SetBitmap(wx.Image(self.prefix1+'application-exit.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap())
		file.AppendItem(quit)
		forced_quit = wx.MenuItem(file, wx.NewId(), "&Forced Quit\tCtrl+Shift+Q", "Force the closure of the program")
		file.AppendItem(forced_quit)
		
		self.edit = wx.Menu()
		undo = wx.MenuItem(self.edit, self.undo_id, "&Undo\tCtrl+Z", "Undo the last action")
		undo.SetBitmap(wx.Image(self.prefix1+'undo.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap())
		self.edit.AppendItem(undo)
		redo = wx.MenuItem(self.edit, self.redo_id, "&Redo\tCtrl+Y", "Redo the last action")
		redo.SetBitmap(wx.Image(self.prefix1+'edit-redo.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap())
		self.edit.AppendItem(redo)
		self.edit.AppendSeparator()
		cut = wx.MenuItem(self.edit, self.cut_id, "&Cut\tCtrl+X", "Cut the selection")
		cut.SetBitmap(wx.Image(self.prefix1+'edit-cut.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap())
		self.edit.AppendItem(cut)
		copy = wx.MenuItem(self.edit, self.copy_id, "&Copy\tCtrl+C", "Copy the selection")
		copy.SetBitmap(wx.Image(self.prefix1+'edit-copy.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap())
		self.edit.AppendItem(copy)
		paste = wx.MenuItem(self.edit, self.paste_id, "&Paste\tCtrl+V", "Paste the text")
		paste.SetBitmap(wx.Image(self.prefix1+'edit-paste.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap())
		self.edit.AppendItem(paste)
		self.edit.AppendSeparator()
		select_all = wx.MenuItem(self.edit, self.select_all_id, "Select &All\tCtrl+A", "Select all in the document")
		select_all.SetBitmap(wx.Image(self.prefix1+'edit-select-all.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap())
		self.edit.AppendItem(select_all)
		
		self.language = wx.Menu()
		text = wx.MenuItem(self.language, wx.NewId(), "&Text", "Change the current language to normal text")
		self.language.AppendItem(text)
		python = wx.MenuItem(self.language, wx.NewId(), "&Python", "Change the current language to Python")
		self.language.AppendItem(python)
		cpp = wx.MenuItem(self.language, wx.NewId(), "&C / C++", "Change the current language to C++")
		self.language.AppendItem(cpp)

		self.buffers = wx.Menu()
		previous = wx.MenuItem(self.buffers, wx.NewId(), "&Previous\tShift+F1", "Change the selection to the previous page")
		previous.SetBitmap(wx.Image(self.prefix1+'go-previous.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap())
		self.buffers.AppendItem(previous)
		next = wx.MenuItem(self.buffers, wx.NewId(), "&Next\tF1", "Change the selection to the next page")
		next.SetBitmap(wx.Image(self.prefix1+'go-next.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap())
		self.buffers.AppendItem(next)
		self.buffers.AppendSeparator()
		if len(sys.argv) == 1:
			y = wx.MenuItem(self.buffers, wx.NewId(), self.name+repr(self.untitled), "Change the page to "+self.name+repr(self.untitled))
			self.buffers.AppendItem(y)
		
		help = wx.Menu()
		about = wx.MenuItem(help, wx.NewId(), "&About...", "Show the informations about the program")
		about.SetBitmap(wx.Image(self.prefix1+"gtk-about.png", wx.BITMAP_TYPE_PNG).ConvertToBitmap())
		help.AppendItem(about)
		help.AppendSeparator()
		bug = wx.MenuItem(help, wx.NewId(), "&Report Bug...", "Open the dialog to report a bug")
		bug.SetBitmap(wx.Image(self.prefix1+"gtk-apport.png", wx.BITMAP_TYPE_PNG).ConvertToBitmap())
		help.AppendItem(bug)
		home = wx.MenuItem(help, wx.NewId(), "&Homepage...", "Open the homepage in the default browser")
		home.SetBitmap(wx.Image(self.prefix1+"gtk-home.png", wx.BITMAP_TYPE_PNG).ConvertToBitmap())
		help.AppendItem(home)
		
		menubar.Append(file, "&File")
		menubar.Append(self.edit, "&Edit")
		menubar.Append(self.language, "&Language")
		menubar.Append(self.buffers, "&Buffers")
		menubar.Append(help, "&Help")
		
		vbox = wx.BoxSizer(wx.VERTICAL)
		self.toolbar = self.CreateToolBar(wx.TB_HORIZONTAL)
		self.toolbar.AddSimpleTool(id1, wx.Image(self.prefix2+'document-new.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'New', 'Open a new file')
		self.toolbar.AddSimpleTool(id2, wx.Image(self.prefix2+'document-save.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Save', 'Save the current file')
		self.toolbar.AddSimpleTool(id3, wx.Image(self.prefix2+'document-save-as.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Save As', 'Save the current file under a new name')
		self.toolbar.AddSimpleTool(id4, wx.Image(self.prefix2+'document-open.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Open', 'Open a file')
		self.toolbar.AddSimpleTool(id8, wx.Image(self.prefix2+'window-close.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Close', 'Close the current tab')
		self.toolbar.AddSeparator()
		self.toolbar.AddSimpleTool(self.id6, wx.Image(self.prefix2+'edit-undo.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Undo', 'Undo the last action')
		self.toolbar.AddSimpleTool(self.id7, wx.Image(self.prefix2+'edit-redo.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Redo', 'Redo the last action')
		self.toolbar.AddSeparator()
		self.toolbar.AddSimpleTool(id11, wx.Image(self.prefix2+'edit-cut.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Cut', 'Cut the selection')
		self.toolbar.AddSimpleTool(id12, wx.Image(self.prefix2+'edit-copy.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Copy', 'Copy the selection')
		self.toolbar.AddSimpleTool(id13, wx.Image(self.prefix2+'edit-paste.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Paste', 'Paste the text')
		self.toolbar.AddSeparator()
		self.toolbar.AddSimpleTool(id9, wx.Image(self.prefix2+'go-previous.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Previous', 'Change the selection to the previous page')
		self.toolbar.AddSimpleTool(id10, wx.Image(self.prefix2+'go-next.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Next', 'Change the selection to the next page')
		self.toolbar.AddSeparator()
		self.toolbar.AddSimpleTool(id5, wx.Image(self.prefix2+'application-exit.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Quit', 'Close the program')
		self.toolbar.Realize()
		
		self.nb = wx.Notebook(self, wx.NewId())
		
		self.parse = functions.parse(sys.argv, self)
		if not self.parse:
			sys.exit(0)
		
		if " *" in self.nb.GetPageText(self.nb.GetSelection()):
			self.SetTitle("%s wxEdit" % self.nb.GetPageText(self.nb.GetSelection()))
		else:
			self.SetTitle("%s - wxEdit" % self.nb.GetPageText(self.nb.GetSelection()))
		if self.nb.GetCurrentPage().CanUndo():
			self.toolbar.EnableTool(self.id6, True)
			self.edit.Enable(self.undo_id, True)
		else:
			self.toolbar.EnableTool(self.id6, False)
			self.edit.Enable(self.undo_id, False)
		if self.nb.GetCurrentPage().CanRedo():
			self.toolbar.EnableTool(self.id7, True)
			self.edit.Enable(self.redo_id, True)
		else:
			self.toolbar.EnableTool(self.id7, False)
			self.edit.Enable(self.redo_id, False)
		
		vbox.Add(self.nb, 1, wx.EXPAND | wx.TOP, border=5)
		
		self.SetMenuBar(menubar)
		self.SetSizer(vbox)
		self.OnReload()
		self.Show()
		
		self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnChangePage, id=self.nb.GetId())
		self.Bind(stc.EVT_STC_CHANGE, self.OnModify, id=self.nb.GetCurrentPage().GetId())
		self.Bind(wx.EVT_MENU_OPEN, self.OnOpenMenu)
		self.Bind(wx.EVT_MENU, self.OnNew, id=new.GetId())
		self.Bind(wx.EVT_MENU, self.OnSave, id=save.GetId())
		self.Bind(wx.EVT_MENU, self.OnSaveAs, id=save_as.GetId())
		self.Bind(wx.EVT_MENU, self.OnOpen, id=open.GetId())
		self.Bind(wx.EVT_MENU, self.OnClose, id=close.GetId())
		self.Bind(wx.EVT_MENU, self.OnQuit, id=quit.GetId())
		self.Bind(wx.EVT_MENU, self.OnUndo, id=undo.GetId())
		self.Bind(wx.EVT_MENU, self.OnRedo, id=redo.GetId())
		self.Bind(wx.EVT_MENU, self.OnAbout, id=about.GetId())
		self.Bind(wx.EVT_MENU, self.OnForcedQuit, id=forced_quit.GetId())
		self.Bind(wx.EVT_MENU, self.OnSelectAll, id=self.select_all_id)
		self.Bind(wx.EVT_MENU, self.OnPrevious, id=previous.GetId())
		self.Bind(wx.EVT_MENU, self.OnNext, id=next.GetId())
		for y in range(3, self.buffers.GetMenuItemCount()):
			if self.buffers.FindItemByPosition(y):
				self.Bind(wx.EVT_MENU, self.OnChangeSelection, id=self.buffers.FindItemByPosition(y).GetId())
		self.Bind(wx.EVT_MENU, self.OnSaveAll, id=save_all.GetId())
		self.Bind(wx.EVT_MENU, self.OnCloseAll, id=close_all.GetId())
		self.Bind(wx.EVT_MENU, self.OnCopy, id=copy.GetId())
		self.Bind(wx.EVT_MENU, self.OnCut, id=cut.GetId())
		self.Bind(wx.EVT_MENU, self.OnPaste, id=paste.GetId())
		self.Bind(wx.EVT_MENU, self.OnSaveSession, id=save_session.GetId())
		self.Bind(wx.EVT_MENU, self.OnLoadSession, id=load_session.GetId())
		self.Bind(wx.EVT_MENU, self.OnOpenOptionsFile, id=oof.GetId())
		self.Bind(wx.EVT_MENU, self.OnSavePreferences, id=save_preferences.GetId())
		self.Bind(wx.EVT_MENU, self.OnLoadPreferences, id=load_preferences.GetId())
		self.Bind(wx.EVT_MENU, self.OnPython, id=python.GetId())
		self.Bind(wx.EVT_MENU, self.OnText, id=text.GetId())
		self.Bind(wx.EVT_MENU, self.OnBug, id=bug.GetId())
		self.Bind(wx.EVT_MENU, self.OnHome, id=home.GetId())
		self.Bind(wx.EVT_MENU, self.OnCpp, id=cpp.GetId())
		self.Bind(wx.EVT_MENU, self.OnReload, id=reload_preferences.GetId())
		self.Bind(wx.EVT_TOOL, self.OnNew, id=id1)
		self.Bind(wx.EVT_TOOL, self.OnOpen, id=id4)
		self.Bind(wx.EVT_TOOL, self.OnSave, id=id2)
		self.Bind(wx.EVT_TOOL, self.OnSaveAs, id=id3)
		self.Bind(wx.EVT_TOOL, self.OnQuit, id=id5)
		self.Bind(wx.EVT_TOOL, self.OnUndo, id=self.id6)
		self.Bind(wx.EVT_TOOL, self.OnRedo, id=self.id7)
		self.Bind(wx.EVT_TOOL, self.OnClose, id=id8)
		self.Bind(wx.EVT_TOOL, self.OnPrevious, id=id9)
		self.Bind(wx.EVT_TOOL, self.OnNext, id=id10)
		self.Bind(wx.EVT_TOOL, self.OnCut, id=id11)
		self.Bind(wx.EVT_TOOL, self.OnCopy, id=id12)
		self.Bind(wx.EVT_TOOL, self.OnPaste, id=id13)
		self.Bind(wx.EVT_CHAR, self.OnChar, id=self.GetId())
		self.Bind(wx.EVT_CLOSE, self.OnQuitX, id=self.GetId())
	def OnAbout(self, event):
		info = wx.AboutDialogInfo()
		info.SetIcon(wx.Icon(self.prefix3+'wxsoft.png', wx.BITMAP_TYPE_PNG))
		info.SetName('wxSoft')
		info.SetVersion(self.version)
		info.SetDescription("""wxSoft is a combination of Editor, client BitTorrent, ... written in Python (GUI - wxPython)""")
		info.SetCopyright('(C) 2009 %s' % self.author)
		info.SetWebSite(self.site)
		info.SetLicence("""Copyright (C) 2009  Alessio "Alexei95" Colucci

wxSoft is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.

wxSoft is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with wxSoft. If not, see <http://www.gnu.org/licenses/>.""")
		info.AddDeveloper('%s\n\t%s\n\t(Developer)' % (self.author, self.email))
		info.AddDocWriter('%s\n\t%s' % (self.author, self.email))
		info.AddArtist('%s\n\t%s' % (self.author, self.email))
		info.AddTranslator('%s\n\t%s' % (self.author, self.email))
		wx.AboutBox(info)
	def OnBug(self, event=None):
		bug = lib.classes.BugDialog(self)
	def OnChangePage(self, event):
		if " *" in self.nb.GetPageText(self.nb.GetSelection()):
			self.SetTitle("%s wxEdit" % self.nb.GetPageText(self.nb.GetSelection()))
		else:
			self.SetTitle("%s - wxEdit" % self.nb.GetPageText(self.nb.GetSelection()))
		if self.nb.GetCurrentPage().GetReadOnly():
			self.ro.Check(True)
		if self.nb.GetCurrentPage().CanUndo():
			self.toolbar.EnableTool(self.id6, True)
			self.edit.Enable(self.undo_id, True)
		else:
			self.toolbar.EnableTool(self.id6, False)
			self.edit.Enable(self.undo_id, False)
		if self.nb.GetCurrentPage().CanRedo():
			self.toolbar.EnableTool(self.id7, True)
			self.edit.Enable(self.redo_id, True)
		else:
			self.toolbar.EnableTool(self.id7, False)
			self.edit.Enable(self.redo_id, False)
	def OnChangeSelection(self, event):
		for x in range(self.nb.GetPageCount()):
			if self.nb.GetPageText(x) == self.buffers.FindItemById(event.GetId()).GetText():
				val = x
		self.nb.SetSelection(val)
		if " *" in self.nb.GetPageText(val):
			self.SetTitle("%s wxEdit" % self.nb.GetPageText(val))
		else:
			self.SetTitle("%s - wxEdit" % self.nb.GetPageText(val))
		if self.nb.GetCurrentPage().GetReadOnly():
			self.ro.Check(True)
		if self.nb.GetCurrentPage().CanUndo():
			self.toolbar.EnableTool(self.id6, True)
			self.edit.Enable(self.undo_id, True)
		else:
			self.toolbar.EnableTool(self.id6, False)
			self.edit.Enable(self.undo_id, False)
		if self.nb.GetCurrentPage().CanRedo():
			self.toolbar.EnableTool(self.id7, True)
			self.edit.Enable(self.redo_id, True)
		else:
			self.toolbar.EnableTool(self.id7, False)
			self.edit.Enable(self.redo_id, False)
	def OnChar(self, event):
		keycode = event.GetKeyCode()
		shift = event.ShiftDown()
		if keycode == 17 and not shift:
			self.Close()
		elif keycode == 19 and not shift:
			self.OnSave()
		elif keycode == 19  and shift:
			self.OnSaveAs()
		elif keycode == 15:
			self.OnOpen()
		elif keycode == 14:
			self.OnNew()
		elif keycode == 26:
			self.OnUndo()
		elif keycode == 25:
			self.OnRedo()
		elif keycode == 17 and shift:
			self.OnForcedQuit()
		event.Skip()
	def OnClose(self, event=None):
		if "*" in self.nb.GetPageText(self.nb.GetSelection()):
			dlg = wx.MessageDialog(self, "Do you want to save changes at '"+self.nb.GetPageText(self.nb.GetSelection())[:-2]+"'?", "Question", wx.YES_NO | wx.CANCEL | wx.YES_DEFAULT | wx.ICON_QUESTION)
			val = dlg.ShowModal()
			if val == wx.ID_YES:
				self.OnSave()
			elif val == wx.ID_CANCEL:
				return
		for y in range(3, self.buffers.GetMenuItemCount()):
			if self.buffers.FindItemByPosition(y):
				if self.nb.GetPageText(self.nb.GetSelection())[-2:] == " *":
					if self.nb.GetPageText(self.nb.GetSelection())[:-2] == self.buffers.FindItemByPosition(y).GetText():
						self.buffers.DestroyItem(self.buffers.FindItemByPosition(y))
				else:
					if self.nb.GetPageText(self.nb.GetSelection()) == self.buffers.FindItemByPosition(y).GetText():
						self.buffers.DestroyItem(self.buffers.FindItemByPosition(y))
		self.nb.RemovePage(self.nb.GetSelection())
		if self.doc == 1:
			self.OnNew()
		else:
			self.doc -= 1
		if " *" in self.nb.GetPageText(self.nb.GetSelection()):
			self.SetTitle("%s wxEdit" % self.nb.GetPageText(self.nb.GetSelection()))
		else:
			self.SetTitle("%s - wxEdit" % self.nb.GetPageText(self.nb.GetSelection()))
	def OnCloseAll(self, event):
		page_remove = []
		menu_remove = []
		for x in range(self.nb.GetPageCount()):
			for y in range(3, self.buffers.GetMenuItemCount()):
				if self.buffers.FindItemByPosition(y):
					menu_remove.append(self.buffers.FindItemByPosition(y))
			page_remove.append(x)
		for x in page_remove:
			self.nb.RemovePage(0)
		for x in menu_remove:
			self.buffers.DestroyItem(x)
		self.doc = 1
		self.OnNew()
		if " *" in self.nb.GetPageText(self.nb.GetSelection()):
			self.SetTitle("%s wxEdit" % self.nb.GetPageText(self.nb.GetSelection()))
		else:
			self.SetTitle("%s - wxEdit" % self.nb.GetPageText(self.nb.GetSelection()))
	def OnCopy(self, event):
		self.nb.GetCurrentPage().Copy()
	def OnCpp(self, event=None):
		panel = self.nb.GetCurrentPage()
		if panel.GetLexer() != stc.STC_LEX_CPP:
			panel.SetLexer(stc.STC_LEX_CPP)
			panel.SetKeyWords(0, Frame.cpp_key)
			
			panel.StyleSetSpec(stc.STC_C_DEFAULT, "fore:#808080,face:%(helv)s,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_C_COMMENT, "fore:#007F00,face:%(other)s,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_C_NUMBER, "fore:#007F7F,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_C_STRING, "fore:#7F007F,italic,face:%(times)s,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_C_CHARACTER, "fore:#7F007F,italic,face:%(times)s,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_C_WORD, "fore:#00007F,bold,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_C_GLOBALCLASS, "fore:#0000FF,bold,underline,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_C_OPERATOR, "bold,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_C_IDENTIFIER, "fore:#808080,face:%(helv)s,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_C_COMMENTLINE, "fore:#7F7F7F,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_C_STRINGEOL, "fore:#000000,face:%(mono)s,back:#E0C0E0,eol,size:%(size)d" % Frame.faces)
	def OnCut(self, event):
		self.nb.GetCurrentPage().Cut()
	def OnForcedQuit(self, event):
		sys.exit(0)
	def OnHome(self, event=None):
		webbrowser.open(self.site)
	def OnLanguage(self, file):
		if self.nb.GetPage(file).GetLexer() == 0:
			ext = os.path.basename(self.nb.GetPageText(file)).split(".")
			if len(ext) == 2:
				if ext[1] in ("py", "pyw"):
					self.OnPython()
				elif ext[1] in ("c","cc", "cpp", "cxx", "h", "hh", "hpp", "hxx"):
					self.OnCpp()
				elif ext[1] in ("txt"):
					self.OnText()
			elif len(ext) == 1:
				self.OnText()
	def OnLoadPreferences(self, event):
		dlg = wx.FileDialog(self, "Load Preferences", self.home, "", "Preferences files|*.ini|All files|*.*", wx.OPEN)
		if dlg.ShowModal() == wx.ID_OK:
			self.OnReload(dlg.GetPath())
	def OnLoadSession(self, event):
		dlg = wx.FileDialog(self, "Load Session", self.home, "", "All files|*.*", wx.OPEN)
		if dlg.ShowModal() == wx.ID_OK:
			try:
				f = open(dlg.GetPath(), "r")
			except IOError:
				dlg3 = wx.MessageDialog("Cannot load session "+dlg.GetPath(), "Error", wx.OK | wx.ICON_ERROR)
				dlg3.ShowModal()
				return
			self.doc = int(f.readline()[:-1])
			while 1:
				line = f.readline()
				if line == "":
					break
				line = line[:-1]
				panel = Sheet(self.nb)
				panel.LoadFile(line)
				self.nb.AddPage(panel, line)
				y = wx.MenuItem(self.buffers, wx.NewId(), line, "Change page to "+line)
				self.buffers.AppendItem(y)
			self.nb.ChangeSelection(self.nb.GetPageCount() - 1)
			f.close()
			dlg2 = wx.MessageDialog(self, "Session loaded correctly", "Load Session", wx.OK | wx.ICON_INFORMATION)
			if dlg2.ShowModal() == wx.ID_OK:
				dlg.Destroy()
				dlg2.Destroy()
	def OnModify(self, event):
		self.nb.GetCurrentPage().EndUndoAction()
		self.nb.GetCurrentPage().BeginUndoAction()
		if self.nb.GetCurrentPage().GetModify() and not "*" in self.nb.GetPageText(self.nb.GetSelection()):
			self.nb.SetPageText(self.nb.GetSelection(), self.nb.GetPageText(self.nb.GetSelection())+" *")
		if self.nb.GetCurrentPage().CanUndo():
			self.toolbar.EnableTool(self.id6, True)
			self.edit.Enable(self.undo_id, True)
		else:
			self.toolbar.EnableTool(self.id6, False)
			self.edit.Enable(self.undo_id, False)
		if self.nb.GetCurrentPage().CanRedo():
			self.toolbar.EnableTool(self.id7, True)
			self.edit.Enable(self.redo_id, True)
		else:
			self.toolbar.EnableTool(self.id7, False)
			self.edit.Enable(self.redo_id, False)
	def OnNew(self, event=None):
		if event:
			self.doc += 1
		panel = Sheet(self.nb)
		self.OnReload()
		self.untitled += 1
		self.nb.AddPage(panel, self.name+repr(self.untitled))
		self.nb.ChangeSelection(self.nb.GetPageCount() - 1)
		y = wx.MenuItem(self.buffers, wx.NewId(), self.name+repr(self.untitled), "Change page to "+self.name+repr(self.untitled))
		self.buffers.AppendItem(y)
		self.Bind(wx.EVT_MENU, self.OnChangeSelection, id=y.GetId())
		if " *" in self.nb.GetPageText(self.nb.GetSelection()):
			self.SetTitle("%s wxEdit" % self.nb.GetPageText(self.nb.GetSelection()))
		else:
			self.SetTitle("%s - wxEdit" % self.nb.GetPageText(self.nb.GetSelection()))
		if self.nb.GetCurrentPage().CanUndo():
			self.toolbar.EnableTool(self.id6, True)
			self.edit.Enable(self.undo_id, True)
		else:
			self.toolbar.EnableTool(self.id6, False)
			self.edit.Enable(self.undo_id, False)
		if self.nb.GetCurrentPage().CanRedo():
			self.toolbar.EnableTool(self.id7, True)
			self.edit.Enable(self.redo_id, True)
		else:
			self.toolbar.EnableTool(self.id7, False)
			self.edit.Enable(self.redo_id, False)
	def OnNext(self, event):
		if self.nb.GetSelection() == (self.nb.GetPageCount() - 1):
			self.nb.SetSelection(0)
		else:
			self.nb.SetSelection(self.nb.GetSelection() + 1)
		if " *" in self.nb.GetPageText(self.nb.GetSelection()):
			self.SetTitle("%s wxEdit" % self.nb.GetPageText(self.nb.GetSelection()))
		else:
			self.SetTitle("%s - wxEdit" % self.nb.GetPageText(self.nb.GetSelection()))
		if self.nb.GetCurrentPage().GetReadOnly():
			self.ro.Check(True)
		if self.nb.GetCurrentPage().CanUndo():
			self.toolbar.EnableTool(self.id6, True)
			self.edit.Enable(self.undo_id, True)
		else:
			self.toolbar.EnableTool(self.id6, False)
			self.edit.Enable(self.undo_id, False)
		if self.nb.GetCurrentPage().CanRedo():
			self.toolbar.EnableTool(self.id7, True)
			self.edit.Enable(self.redo_id, True)
		else:
			self.toolbar.EnableTool(self.id7, False)
			self.edit.Enable(self.redo_id, False)
	def OnOpen(self, event=None, filename=None, readonly=False, first=False):
		if first:
			self.doc = 0
		if event:
			dlg = wx.FileDialog(self, "Open File", self.home, "", "All files|*.*", wx.OPEN)
			if dlg.ShowModal() == wx.ID_OK:
				opened = []
				for x in range(self.nb.GetPageCount()):
					if "*" not in self.nb.GetPageText(x):
						opened.append(self.nb.GetPageText(x))
					else:
						opened.append(self.nb.GetPageText(x)[:-2])
				if dlg.GetPath() not in opened:
					panel = Sheet(self.nb)
					self.OnReload()
					if not panel.LoadFile(dlg.GetPath()):
						dlg44 = wx.MessageDialog("Cannot open "+dlg.GetPath(), "Error", wx.OK | wx.ICON_ERROR)
						dlg44.ShowModal()
						return
					self.nb.AddPage(panel, dlg.GetPath())
					self.nb.ChangeSelection(self.nb.GetPageCount() - 1)
					self.OnLanguage(self.nb.GetSelection())
					self.doc += 1
				y = wx.MenuItem(self.buffers, wx.NewId(), dlg.GetPath(), "Change page to "+dlg.GetPath())
				self.buffers.AppendItem(y)
				self.Bind(wx.EVT_MENU, self.OnChangeSelection, id=y.GetId())
		elif filename:
			panel = Sheet(self.nb)
			if os.path.dirname(filename) == "":
				filename = os.path.join(os.getcwd(), filename)
			self.OnReload()
			if not panel.LoadFile(filename):
				dlg44 = wx.MessageDialog("Cannot open "+filename, "Error", wx.OK | wx.ICON_ERROR)
				dlg44.ShowModal()
				return
			self.nb.AddPage(panel, filename)
			self.nb.ChangeSelection(self.nb.GetPageCount() - 1)
			self.OnLanguage(self.nb.GetSelection())
			self.doc += 1
			y = wx.MenuItem(self.buffers, wx.NewId(), filename, "Change page to "+filename)
			self.buffers.AppendItem(y)
			self.Bind(wx.EVT_MENU, self.OnChangeSelection, id=y.GetId())
		else:
			panel = None
		if readonly or self.cfg.cfg.getboolean("Options", "ReadOnly"):
			if panel:
				panel.SetReadOnly(True)
		if " *" in self.nb.GetPageText(self.nb.GetSelection()):
			self.SetTitle("%s wxEdit" % self.nb.GetPageText(self.nb.GetSelection()))
		else:
			self.SetTitle("%s - wxEdit" % self.nb.GetPageText(self.nb.GetSelection()))
		if self.nb.GetCurrentPage().CanUndo():
			self.toolbar.EnableTool(self.id6, True)
			self.edit.Enable(self.undo_id, True)
		else:
			self.toolbar.EnableTool(self.id6, False)
			self.edit.Enable(self.undo_id, False)
		if self.nb.GetCurrentPage().CanRedo():
			self.toolbar.EnableTool(self.id7, True)
			self.edit.Enable(self.redo_id, True)
		else:
			self.toolbar.EnableTool(self.id7, False)
			self.edit.Enable(self.redo_id, False)
	def OnOpenMenu(self, event):
		if self.nb.GetCurrentPage().GetSelectedText() != "":
			self.edit.Enable(self.cut_id, True)
			self.edit.Enable(self.copy_id, True)
		else:
			self.edit.Enable(self.cut_id, False)
			self.edit.Enable(self.copy_id, False)
		if self.nb.GetCurrentPage().CanPaste():
			self.edit.Enable(self.paste_id, True)
		else:
			self.edit.Enable(self.paste_id, False)
	def OnOpenOptionsFile(self, event):
		self.OnOpen(filename=self.cfg.cfg_path)
	def OnPaste(self, event):
		self.nb.GetCurrentPage().Paste()
	def OnPrevious(self, event):
		if self.nb.GetSelection() == 0:
			self.nb.SetSelection(self.nb.GetPageCount() - 1)
		else:
			self.nb.SetSelection(self.nb.GetSelection() - 1)
		if " *" in self.nb.GetPageText(self.nb.GetSelection()):
			self.SetTitle("%s wxEdit" % self.nb.GetPageText(self.nb.GetSelection()))
		else:
			self.SetTitle("%s - wxEdit" % self.nb.GetPageText(self.nb.GetSelection()))
		if self.nb.GetCurrentPage().GetReadOnly():
			self.ro.Check(True)
		if self.nb.GetCurrentPage().CanUndo():
			self.toolbar.EnableTool(self.id6, True)
			self.edit.Enable(self.undo_id, True)
		else:
			self.toolbar.EnableTool(self.id6, False)
			self.edit.Enable(self.undo_id, False)
		if self.nb.GetCurrentPage().CanRedo():
			self.toolbar.EnableTool(self.id7, True)
			self.edit.Enable(self.redo_id, True)
		else:
			self.toolbar.EnableTool(self.id7, False)
			self.edit.Enable(self.redo_id, False)
	def OnPython(self, event=None):
		panel = self.nb.GetCurrentPage()
		if panel.GetLexer() != stc.STC_LEX_PYTHON:
			panel.SetLexer(stc.STC_LEX_PYTHON)
			panel.SetKeyWords(0, " ".join(keyword.kwlist))
			
			panel.StyleSetSpec(stc.STC_P_DEFAULT, "fore:#808080,face:%(helv)s,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_P_COMMENTLINE, "fore:#007F00,face:%(other)s,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_P_NUMBER, "fore:#007F7F,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_P_STRING, "fore:#7F007F,italic,face:%(times)s,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_P_CHARACTER, "fore:#7F007F,italic,face:%(times)s,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_P_WORD, "fore:#00007F,bold,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_P_TRIPLE, "fore:#7F0000,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_P_TRIPLEDOUBLE, "fore:#7F0000,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_P_CLASSNAME, "fore:#0000FF,bold,underline,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_P_DEFNAME, "fore:#007F7F,bold,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_P_OPERATOR, "bold,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_P_IDENTIFIER, "fore:#808080,face:%(helv)s,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_P_COMMENTBLOCK, "fore:#7F7F7F,size:%(size)d" % Frame.faces)
			panel.StyleSetSpec(stc.STC_P_STRINGEOL, "fore:#000000,face:%(mono)s,back:#E0C0E0,eol,size:%(size)d" % Frame.faces)
	def OnQuit(self, event=None):
		self.cfg.config_write(self)
		not_saved = []
		if self.nb.GetPageCount() == 1:
			if " *" in self.nb.GetPageText(0):
				dlg = wx.MessageDialog(self, "Do you want to save changes at '"+self.nb.GetPageText(0)[:-2]+"'?", "Question", wx.YES_NO | wx.CANCEL | wx.YES_DEFAULT | wx.ICON_QUESTION)
				val = dlg.ShowModal()
				if val == wx.ID_YES:
					self.OnSave()
					#if self.st.IsChecked():
					#	self.tskic.Destroy()
					#dlg.Destroy()
					#self.Destroy()
					sys.exit(0)
				elif val == wx.ID_NO:
					#if self.st.IsChecked():
					#	self.tskic.Destroy()
					#dlg.Destroy()
					#self.Destroy()
					sys.exit(0)
				elif val == wx.ID_CANCEL:
					event.Skip()
			else:
				#if self.st.IsChecked():
				#	self.tskic.Destroy()
				#self.Destroy()
				sys.exit(0)
		else:			
			for x in range(self.nb.GetPageCount()):
				if "*" in self.nb.GetPageText(x):
					not_saved.append(True)
				else:
					not_saved.append(False)
			if True in not_saved:
				dlg3 = wx.MessageDialog(self, "There are some files which aren't saved.\nDo you want to save and exit?", "Question", wx.YES_NO | wx.CANCEL | wx.YES_DEFAULT | wx.ICON_QUESTION)
				val = dlg3.ShowModal()
				if val == wx.ID_YES:
					file_not_saved = []
					not_saved2 = not_saved
					should = []
					for x in not_saved:
						if x:
							if "*" in self.nb.GetPageText(not_saved.index(x)):
								file_not_saved.append(self.nb.GetPageText(not_saved.index(x))[:-2])
							else:
								file_not_saved.append(self.nb.GetPageText(not_saved.index(x)))
							should.append(x)
						not_saved[not_saved.index(x)] = None
					dlg = wx.MultiChoiceDialog(self, "Select the file(s) to save", "File not saved", file_not_saved)
					dlg.SetSelections(should)
					if dlg.ShowModal() == wx.ID_OK:
						file_selected = dlg.GetSelections()
						file_not_saved2 = file_not_saved
						file_not_saved = []
						for x in file_selected:
							file_not_saved.append(file_not_saved2[x])
						file_correct_saved = []
						file_uncorrect_saved = []
						path = ""
						for x in file_not_saved:
							if "Untitled" in x:
								for y in range(self.nb.GetPageCount()):
									if x == self.nb.GetPageText(y):
										z = y
								dlg = wx.FileDialog(self, "Save File", self.home, "", "All files|*.*", wx.SAVE | wx.OVERWRITE_PROMPT)
								if dlg.ShowModal() == wx.ID_OK:
									self.nb.GetPage(z).SaveFile(os.path.join(dlg.GetDirectory(), dlg.GetFilename()))
									self.nb.SetPageText(z, os.path.join(dlg.GetDirectory(), dlg.GetFilename()))
									file_correct_saved.append(x)
									path += os.path.join(dlg.GetDirectory(), dlg.GetFilename())+", "
							else:
								for y in range(self.nb.GetPageCount()):
									if self.nb.GetPageText(y) == x:
										self.nb.GetPage(y).SaveFile(x)
										self.nb.SetPageText(y, x)
										file_correct_saved.append(x)
						for x in file_not_saved:
							if x not in file_correct_saved:
								file_uncorrect_saved.append(x)
						file1 = ""
						for x in file_correct_saved:
							if "Untitled" in x:
								file1 += x+" ("+path.split(", ")[file_correct_saved.index(x)]+")\n"
							else:
								file1 += x+"\n"
						file2 = ""
						for x in file_uncorrect_saved:
							file2 += x+"\n"
						dlg2 = wx.MessageDialog(self, "This/ese file(s) is/are correctly saved:\n"+file1+"\nThis/ese file(s) isn't/aren't correctly saved:\n"+file2, "File correctly and uncorrectly saved", wx.OK | wx.ICON_INFORMATION)
						if dlg2.ShowModal() == wx.ID_OK:
							#if self.st.IsChecked():
							#	self.tskic.Destroy()
							#dlg2.Destroy()
							#self.Destroy()
							sys.exit(0)
				elif val == wx.ID_NO:
					#if self.st.IsChecked():
					#	self.tskic.Destroy()
					#dlg3.Destroy()
					#self.Destroy()
					sys.exit(0)
				else:
					event.Skip()
			else:
				#if self.st.IsChecked():
				#	self.tskic.Destroy()
				#self.Destroy()
				sys.exit(0)
	def OnQuitX(self, event):
		if self.st.IsChecked():
			self.Hide()
		else:
			self.OnQuit()
	def OnRedo(self, event):
		self.nb.GetCurrentPage().EndUndoAction()
		self.nb.GetCurrentPage().Redo()
		if self.nb.GetCurrentPage().CanUndo():
			self.toolbar.EnableTool(self.id6, True)
			self.edit.Enable(self.undo_id, True)
		else:
			self.toolbar.EnableTool(self.id6, False)
			self.edit.Enable(self.undo_id, False)
		if self.nb.GetCurrentPage().CanRedo():
			self.toolbar.EnableTool(self.id7, True)
			self.edit.Enable(self.redo_id, True)
		else:
			self.toolbar.EnableTool(self.id7, False)
			self.edit.Enable(self.redo_id, False)
		self.nb.GetCurrentPage().BeginUndoAction()
	def OnReload(self, event=None, path=None):
		del self.cfg
		if not path:
			self.cfg = classes.Config()
		else:
			self.cfg = classes.Config(path)
		x, y = self.cfg.cfg.get("Window", "Position").split(",")
		x, y = int(x), int(y)
		if (x, y) != (0, 0):
			pos = (x, y)
		else:
			pos = (0, 0)
		x, y = self.cfg.cfg.get("Window", "Size").split(",")
		x, y = int(x), int(y)
		if (x, y) != (600, 500):
			size = (x, y)
		else:
			size = (600, 500)
		self.SetSize(size)
		self.SetPosition(pos)
		if self.cfg.cfg.get("Options", "endoflinemode") == "CR":
			for x in range(self.nb.GetPageCount()):
				self.nb.GetPage(x).SetEOLMode(stc.STC_EOL_CR)
		elif self.cfg.cfg.get("Options", "endoflinemode") == "LF":
			for x in range(self.nb.GetPageCount()):
				self.nb.GetPage(x).SetEOLMode(stc.STC_EOL_LF)
		elif self.cfg.cfg.get("Options", "endoflinemode") == "CR + LF":
			for x in range(self.nb.GetPageCount()):
				self.nb.GetPage(x).SetEOLMode(stc.STC_EOL_CRLF)
		if self.cfg.cfg.getboolean("Options", "wrap"):
			for x in range(self.nb.GetPageCount()):
				self.nb.GetPage(x).SetWrapMode(1)
				self.nb.GetPage(x).SetWrapStartIndent(2)
		else:
			for x in range(self.nb.GetPageCount()):
				self.nb.GetPage(x).SetWrapMode(0)
		if self.cfg.cfg.getboolean("Options", "readonly"):
			for x in range(self.nb.GetPageCount()):
				self.nb.GetPage(x).SetReadOnly(1)
		else:
			for x in range(self.nb.GetPageCount()):
				self.nb.GetPage(x).SetReadOnly(0)
		if self.cfg.cfg.getboolean("Options", "systemtrayicon"):
			if not self.tskic:
				self.tskic = lib.classes.TaskBarIcon(self, "wxedit16.gif")
		else:
			self.tskic.Destroy()
			self.tskic = None
		if self.cfg.cfg.getboolean("View", "statusbar"):
			self.statusbar.Show()
		else:
			self.statusbar.Hide()
		if self.cfg.cfg.getboolean("View", "fullscreen"):
			self.ShowFullScreen(True, wx.FULLSCREEN_NOBORDER | wx.FULLSCREEN_NOCAPTION)
		else:
			self.ShowFullScreen(False)
		if self.cfg.cfg.getboolean("View", "whitespace"):
			for x in range(self.nb.GetPageCount()):
				self.nb.GetPage(x).SetViewWhiteSpace(1)
		else:
			for x in range(self.nb.GetPageCount()):
				self.nb.GetPage(x).SetViewWhiteSpace(0)
		if self.cfg.cfg.getboolean("View", "showline"):
			for x in range(self.nb.GetPageCount()):
				self.nb.GetPage(x).SetMarginWidth(0, 40)
				self.nb.GetPage(x).SetMarginType(0, stc.STC_MARGIN_NUMBER)
		else:
			for x in range(self.nb.GetPageCount()):
				self.nb.GetPage(x).SetMarginWidth(0, 0)
		if self.cfg.cfg.getboolean("View", "toolbar") and not self.cfg.cfg.getboolean("View", "fullscreen"):
			self.toolbar.Enable(True)
		else:
			self.toolbar.Enable(False)
			self.cfg.cfg.set("View", "toolbar", "False")
		if self.cfg.cfg.getboolean("View", "endofline"):
			for x in range(self.nb.GetPageCount()):
				self.nb.GetPage(x).SetViewEOL(True)
		else:
			for x in range(self.nb.GetPageCount()):
				self.nb.GetPage(x).SetViewEOL(False)
	def OnSave(self, event=None):#, file=None):
		#if event:
		if "Untitled" in self.nb.GetPageText(self.nb.GetSelection()):
			dlg = wx.FileDialog(self, "Save File", self.home, "", "All files|*.*", wx.SAVE | wx.OVERWRITE_PROMPT)
			if dlg.ShowModal() == wx.ID_OK:
				saved = self.nb.GetCurrentPage().SaveFile(os.path.join(dlg.GetDirectory(), dlg.GetFilename()))
				if not saved:
					dlg2 = wx.MessageDialog(self, "Cannot save "+self.nb.GetPageText(self.nb.GetSelection())+" under "+dlg.GetPath()+"\nSave it under a different name?", "Error", wx.YES_NO | wx.CANCEL | wx.ICON_ERROR)
					y = dlg2.ShowModal()
					if y == wx.ID_YES:
						dlg3 = wx.FileDialog(self, "Save File", self.home, "", "All files|*.*", wx.SAVE | wx.OVERWRITE_PROMPT)
						if dlg3.ShowModal() == wx.ID_OK:
							self.nb.GetCurrentPage().SaveFile(os.path.join(dlg3.GetDirectory(), dlg3.GetFilename()))
							self.nb.SetPageText(self.nb.GetSelection(), dlg3.GetPath())
							self.OnLanguage(self.nb.GetSelection())
				else:
					self.nb.SetPageText(self.nb.GetSelection(), os.path.join(dlg.GetDirectory(), dlg.GetFilename()))
					self.OnLanguage(self.nb.GetSelection())
		elif "*" in self.nb.GetPageText(self.nb.GetSelection()):
			saved = self.nb.GetCurrentPage().SaveFile(self.nb.GetPageText(self.nb.GetSelection())[:-2])
			if not saved:
				dlg2 = wx.MessageDialog(self, "Cannot save "+self.nb.GetPageText(self.nb.GetSelection())+"\nSave it under a different name?", "Error", wx.YES_NO | wx.CANCEL | wx.ICON_ERROR)
				y = dlg2.ShowModal()
				if y == wx.ID_YES:
					dlg3 = wx.FileDialog(self, "Save File", self.home, "", "All files|*.*", wx.SAVE | wx.OVERWRITE_PROMPT)
					if dlg3.ShowModal() == wx.ID_OK:
						self.nb.GetCurrentPage().SaveFile(os.path.join(dlg3.GetDirectory(), dlg3.GetFilename()))
						self.nb.SetPageText(self.nb.GetSelection(), dlg3.GetPath())
						self.OnLanguage(self.nb.GetSelection())
			else:
				self.nb.SetPageText(self.nb.GetSelection(), self.nb.GetPageText(self.nb.GetSelection())[:-2])
				self.OnLanguage(self.nb.GetSelection())
		#elif file:
		#	if "Untitled" in self.nb.GetPageText(file):
		#		dlg = wx.FileDialog(self, "Save File", self.home, "", "All files|*.*", wx.SAVE | wx.OVERWRITE_PROMPT)
		#		if dlg.ShowModal() == wx.ID_OK:
		#			self.nb.GetPage(file).SaveFile(os.path.join(dlg.GetDirectory(), dlg.GetFilename()))
		#			self.nb.SetPageText(file, os.path.join(dlg.GetDirectory(), dlg.GetFilename()))
		#	elif "*" in self.nb.GetPageText(file):
		#		self.nb.GetPage(file).SaveFile(self.nb.GetPageText(file)[:-2])
		#		self.nb.SetPageText(file, self.nb.GetPageText(self.nb.GetSelection())[:-2])
	def OnSaveAll(self, event):
		for x in range(self.nb.GetPageCount()):
			if "Untitled" in self.nb.GetPageText(x):
				dlg = wx.FileDialog(self, "Save File", self.home, "", "All files|*.*", wx.SAVE | wx.OVERWRITE_PROMPT)
				if dlg.ShowModal() == wx.ID_OK:
					saved = self.nb.GetPage(x).SaveFile(os.path.join(dlg.GetDirectory(), dlg.GetFilename()))
					if not saved:
						dlg2 = wx.MessageDialog(self, "Cannot save "+self.nb.GetPageText(self.nb.GetSelection())+" under "+dlg.GetPath()+"\nSave it under a different name?", "Error", wx.YES_NO | wx.CANCEL | wx.ICON_ERROR)
						y = dlg2.ShowModal()
						if y == wx.ID_YES:
							dlg3 = wx.FileDialog(self, "Save File", self.home, "", "All files|*.*", wx.SAVE | wx.OVERWRITE_PROMPT)
							if dlg3.ShowModal() == wx.ID_OK:
								self.nb.GetCurrentPage().SaveFile(os.path.join(dlg3.GetDirectory(), dlg3.GetFilename()))
								self.nb.SetPageText(self.nb.GetSelection(), dlg3.GetPath())
								self.OnLanguage(self.nb.GetSelection())
					else:
						self.nb.SetPageText(x, os.path.join(dlg.GetDirectory(), dlg.GetFilename()))
						self.OnLanguage(self.nb.GetSelection())
			elif "*" in self.nb.GetPageText(x):
				saved = self.nb.GetPage(x).SaveFile(self.nb.GetPageText(x)[:-2])
				if not saved:
					dlg2 = wx.MessageDialog(self, "Cannot save "+self.nb.GetPageText(self.nb.GetSelection())+" under "+dlg.GetPath()+"\nSave it under a different name?", "Error", wx.YES_NO | wx.CANCEL | wx.ICON_ERROR)
				y = dlg2.ShowModal()
				if y == wx.ID_YES:
					dlg3 = wx.FileDialog(self, "Save File", self.home, "", "All files|*.*", wx.SAVE | wx.OVERWRITE_PROMPT)
					if dlg3.ShowModal() == wx.ID_OK:
						self.nb.GetCurrentPage().SaveFile(os.path.join(dlg3.GetDirectory(), dlg3.GetFilename()))
						self.nb.SetPageText(self.nb.GetSelection(), dlg3.GetPath())
						self.OnLanguage(self.nb.GetSelection())
				else:
					self.nb.SetPageText(x, self.nb.GetPageText(self.nb.GetSelection())[:-2])
					self.OnLanguage(self.nb.GetSelection())
	def OnSaveAs(self, event):
		dlg = wx.FileDialog(self, "Save File", self.home, "", "All files|*.*", wx.SAVE | wx.OVERWRITE_PROMPT)
		if dlg.ShowModal() == wx.ID_OK:
			saved = self.nb.GetCurrentPage().SaveFile(os.path.join(dlg.GetDirectory(), dlg.GetFilename()))
			if not saved:
				dlg2 = wx.MessageDialog(self, "Cannot save "+self.nb.GetPageText(self.nb.GetSelection())+" under "+dlg.GetPath()+"\nSave it under a different name?", "Error", wx.YES_NO | wx.CANCEL | wx.ICON_ERROR)
				y = dlg2.ShowModal()
				if y == wx.ID_YES:
					dlg3 = wx.FileDialog(self, "Save File", self.home, "", "All files|*.*", wx.SAVE | wx.OVERWRITE_PROMPT)
					if dlg3.ShowModal() == wx.ID_OK:
						self.nb.GetCurrentPage().SaveFile(os.path.join(dlg3.GetDirectory(), dlg3.GetFilename()))
						self.nb.SetPageText(self.nb.GetSelection(), dlg3.GetPath())
						self.OnLanguage(self.nb.GetSelection())
			else:
				self.nb.SetPageText(self.nb.GetSelection(), dlg.GetPath())
				self.OnLanguage(self.nb.GetSelection())
	def OnSavePreferences(self, event):
		dlg = wx.FileDialog(self, "Save Preferences", self.home, "", "Preferences files|*.ini|All files|*.*", wx.SAVE | wx.OVERWRITE_PROMPT)
		if dlg.ShowModal() == wx.ID_OK:
			if dlg.GetPath()[-4:] != ".ini":
				saved = self.cfg.onfig_write(dlg.GetPath()+".ini")
			else:
				saved = self.cfg.config_write(dlg.GetPath())
			if not saved:
				dlg2 = wx.MessageDialog(self, "Cannot save the preferences under "+dlg.GetPath()+"\nSave it under a different name?", "Error", wx.YES_NO | wx.CANCEL)
				y = dlg2.ShowModal()
				if y == wx.ID_YES:
					dlg3 = wx.FileDialog(self, "Save Preferences", self.home, "", "All files|*.*", wx.SAVE | wx.OVERWRITE_PROMPT)
					if dlg3.ShowModal() == wx.ID_OK:
						if dlg.GetPath[-4:] != ".ini":
							self.cfg.config_write(dlg.GetPath()+".ini")
						else:
							self.cfg.config_write(dlg.GetPath()+".ini")
	def OnSaveSession(self, event):
		dlg = wx.FileDialog(self, "Save Session", self.home, "", "All files|*.*", wx.SAVE | wx.OVERWRITE_PROMPT)
		if dlg.ShowModal() == wx.ID_OK:
			try:
				f = open(dlg.GetPath(), "w")
			except IOError:
				dlg2 = wx.MessageDialog(self, "Cannot save the session under "+dlg.GetPath()+"\nSave it under a different name?", "Error", wx.YES_NO | wx.CANCEL)
				y = dlg2.ShowModal()
				if y == wx.ID_YES:
					dlg3 = wx.FileDialog(self, "Save Session", self.home, "", "All files|*.*", wx.SAVE | wx.OVERWRITE_PROMPT)
					if dlg3.ShowModal() == wx.ID_OK:
						f = open(dlg3.GetPath(), "w")
				else:
					return
			f.write(repr(self.doc)+"\n")
			for x in range(self.nb.GetPageCount()):
				if "Untitled" not in self.nb.GetPageText(x):
					f.write(self.nb.GetPageText(x)+"\n")
			f.close()
			dlg4 = wx.MessageDialog(self, "Session saved correctly", "Save Session", wx.OK | wx.ICON_INFORMATION)
			if dlg4.ShowModal() == wx.ID_OK:
				dlg.Destroy()
			dlg4.Destroy()
	def OnSelectAll(self, event):
		self.nb.GetPage(self.nb.GetSelection()).SelectAll()
	def OnText(self, event):
		panel = self.nb.GetCurrentPage()
		if panel.GetLexer() != 0:
			panel.SetLexer(0)
			panel.StyleClearAll()
			panel.StyleSetSpec(stc.STC_STYLE_DEFAULT,     "face:%(helv)s,size:%(size)d" % faces)
			panel.StyleSetSpec(stc.STC_STYLE_LINENUMBER,  "back:#C0C0C0,face:%(helv)s,size:%(size2)d" % faces)
			panel.StyleSetSpec(stc.STC_STYLE_CONTROLCHAR, "face:%(other)s" % faces)
			panel.StyleSetSpec(stc.STC_STYLE_BRACELIGHT,  "fore:#FFFFFF,back:#0000FF,bold")
			panel.StyleSetSpec(stc.STC_STYLE_BRACEBAD,    "fore:#000000,back:#FF0000,bold")
	def OnUndo(self, event):
		self.nb.GetCurrentPage().EndUndoAction()
		self.nb.GetCurrentPage().Undo()
		if self.nb.GetCurrentPage().CanUndo():
			self.toolbar.EnableTool(self.id6, True)
			self.edit.Enable(self.undo_id, True)
		else:
			self.toolbar.EnableTool(self.id6, False)
			self.edit.Enable(self.undo_id, False)
		if self.nb.GetCurrentPage().CanRedo():
			self.toolbar.EnableTool(self.id7, True)
			self.edit.Enable(self.redo_id, True)
		else:
			self.toolbar.EnableTool(self.id7, False)
			self.edit.Enable(self.redo_id, False)
		self.nb.GetCurrentPage().BeginUndoAction()
class Sheet(stc.StyledTextCtrl):
	def __init__(self, parent, id=wx.NewId()):
		stc.StyledTextCtrl.__init__(self, parent, id)
		
		self.StyleSetSpec(stc.STC_STYLE_DEFAULT,     "face:%(helv)s,size:%(size)d" % faces)
		self.StyleSetSpec(stc.STC_STYLE_LINENUMBER,  "back:#C0C0C0,face:%(helv)s,size:%(size2)d" % faces)
		self.StyleSetSpec(stc.STC_STYLE_CONTROLCHAR, "face:%(other)s" % faces)
		self.StyleSetSpec(stc.STC_STYLE_BRACELIGHT,  "fore:#FFFFFF,back:#0000FF,bold")
		self.StyleSetSpec(stc.STC_STYLE_BRACEBAD,    "fore:#000000,back:#FF0000,bold")
		
		self.SetMargins(1, 1)
		
		self.BeginUndoAction()